(function ($) {
    function DetailList(element,options){
        this.dbPath = G_Path.path ? G_Path.path : "";
        this.element=element;
        this.contentElement;
        this.toolsElement;
        this.options=options;
        this.parentId=options.id;
        this.fieldInfo=this.options.fieldInfo;
        this.data=[];
        this.start=1;
        this.rowSize=50;

        this.formName="list";
        this.url="/goods/price/json/"+this.parentId;
        this.viewUrl="/goods/view";
        this.deleteUrl="/goods/delete";
        this.editUrl="/goods/edit";

        this.winDialog=null;
        this.dialogStatus =this.options.dialogStatus|| {ADD:1,EDIT:2,VIEW:3};
        this.highlightResult = function () {
            this.style.color = "#00f";
            this.style.backgroundColor = "#ffffd1";
            this.toolsbarButtons = [];
            this.nowUserId = "";
        };
        this.unhighlightResult = function () {
            this.style.color = "#000";
            this.style.backgroundColor = "transparent";
        };
        this.init();

    }

    DetailList.DEFAULTS = {
        title: "详细列表",
        fieldInfo:[
            {"field":"goodsCount","title":"供货量"},
            {"field":"logisticsName","title":"物流方式"},
            {"field":"marketTime","title":"上市时间"},
            {"field":"goodsUnit","title":"计量单位"},
            {"field":"price","title":"价格"},
            {"field":"address","title":"供货地址"}
        ]
    };
    $.extend(DetailList.prototype, {
        init:function(){
            var self=this;
            this.contentElement=$("<div></div>").appendTo(this.element);
            this.toolsElement=$("<div></div>").attr({
                "class":"toolbar"
            }).appendTo(this.element);

            $("<div class='btn-add'></div>").click(function(){
                self.showDialog();
            }).appendTo(this.toolsElement);

            this.get();
        },
        get:function(){
            var self=this;
            var url=this.url;
            if (url.indexOf("?") != -1) {
                url = url + "&seq=" + Math.random();
            }
            else {
                url = url + "?seq=" + Math.random();
            }
            url = url + "&start=" + this.start + "&rowSize=" + this.rowSize;
            var requestData = this.options.param ? this.options.param : {};
            $.get(url, requestData, function (data) {
                var queryPage = $.parseJSON(data);
                $(self.contentElement).html("");
                if (queryPage && queryPage.list) {
                    self.data = queryPage.list;
                    self.draw();
                }

            });
        },
        draw:function(){
            var self=this;

            var table = document.createElement("table");
            table.className = "tb-list table";
            var oRow = table.insertRow(table.rows.length);
            oRow.className = "tb_tr_title";

            for (var j = 0; j < this.fieldInfo.length; j++) {
                var fi = this.fieldInfo[j];
                var oCell = oRow.insertCell(oRow.cells.length);
                oCell.innerHTML = fi.title?fi.title:"";

            }
            for (var i = 0; i < this.data.length; i++) {
                var model = this.data[i];
                var id = model.id;
                var oRow = table.insertRow(table.rows.length);
                oRow.setAttribute("id", id);
                $(oRow).click(function (e) {
                    e.stopPropagation();
                    var tmpId = $(this).attr("id");
                    var strUrl = self.dbPath + self.viewUrl + "/" + tmpId;
                    window.open(strUrl, "_blank");
                });
                oRow.onmouseover = this.highlightResult;
                oRow.onmouseout = this.unhighlightResult;
                for (var j = 0; j < this.fieldInfo.length; j++) {
                    var fi = this.fieldInfo[j];
                    var oCell = oRow.insertCell(oRow.cells.length);
                    var fieldValue = "";
                    if (fi.field == "no") {
                        fieldValue = i + 1;
                    }
                    else {
                        fieldValue = model[fi.field];
                    }
                    if (fi.type && "img" == fi.type) {
                        fieldValue = "<img src='" + fieldValue + "' width='300' height='200'/>"
                    }
                    fieldValue = fieldValue ? fieldValue : "";
                    oCell.innerHTML = fieldValue;
                }
                if(this.options.showDelete){
                    var oCell = oRow.insertCell(oRow.cells.length);

                    var button=document.createElement("button");
                    button.type="button"
                    button.className="btn btn-red";
                    button.innerHTML="编辑";
                    oCell.appendChild(button);

                    button.setAttribute("docId",id);
                    $(button).click(function(e) {
                        e.stopPropagation();
                        var id = $(this).attr("docId");
                        self.showDialog(self.dialogStatus.EDIT,id);
                    });
                    oCell.appendChild(button);

                    var button=document.createElement("button");
                    button.type="button"
                    button.className="btn";
                    button.innerHTML="删除";
                    oCell.appendChild(button);

                    button.setAttribute("docId",id);
                    $(button).click(function(e){
                        e.stopPropagation();
                        var id=$(this).attr("docId");
                        self.deleteDetail(id);
                    });

                }
            }
            $(this.contentElement).append(table);



        },
        show:function(){
            this.get();
            this.winDialog.close();
        },
        close:function(){
            this.winDialog.close();
        },
        deleteDetail:function(id){
            var self=this;
            var url=this.deleteUrl+"/"+id;

            dialog({
                title: '确认消息',
                content: '是否删除?',
                width: 200,
                ok:function(){
                    $.get(url,function(text){
                        self.get();
                    });
                },
                cancel:function(){

                }
            }).show();

        },
        showDialog:function(flag,id){
            var title,url;
            var dialogStatus=flag||this.dialogStatus.ADD;
            switch(dialogStatus){
                case this.dialogStatus.ADD:
                    title="添加价格";
                    url='/goods/price/add/'+this.parentId;
                    break;
                case this.dialogStatus.EDIT:
                    title="编辑";
                    url='/goods/price/edit/'+id;
                    break;
                case this.dialogStatus.VIEW:
                    title="查看";
                    url='/goods/price/view/'+id;
                    break;
                default:
                    title="新建";
                    url='/goods/price/add/id='+this.parentId;
                    break;

            }
            this.winDialog = dialog({
                title: title,
                url: url,
                width:600

            });
            this.winDialog.showModal();
        }

    });
    $.fn.detailList = function (options) {
        var config = $.extend({}, DetailList.DEFAULTS, options);
        var detail = new DetailList(this, config);
        return detail;
    };


})(jQuery)